
import Alamofire
import HandyJSON

//正式环境
let isPrdEnvironment = false

let devDomainUrl = "http://diaosi.taogoo.top/"
let prdDomainUrl = "http://diaosi.taogoo.top/"

var domainUrl:String = isPrdEnvironment ? prdDomainUrl : devDomainUrl

class AppBaseRequest  {
    static let sharedSession: Alamofire.SessionManager = {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        configuration.timeoutIntervalForRequest = 30.0
        configuration.timeoutIntervalForResource = 30.0
        return Alamofire.SessionManager(configuration: configuration)
    }()

    var requestMethod = HTTPMethod.get
    
    var encoding: ParameterEncoding {
        return URLEncoding.default
    }
    
    public func request(_ url:String, params: Parameters?, deserializeClass: HandyJSON.Type!, successHandler: @escaping (_ result: AnyObject?) -> Void, failureHandler: @escaping (_ error: Error?) -> Void) {
        AppBaseRequest.sharedSession.request(domainUrl + url , method: requestMethod, parameters: params, encoding: encoding).responseString { (responseString) in
            if responseString.result.isSuccess {
                if let result = deserializeClass.deserialize(from: responseString.result.value) {
                    if let model = result as? AppBaseModel {
                        if !isPrdEnvironment {
                            print( " ----------- ")
                            print( "\(params ?? [ : ] ) \n ")
                            print( responseString.result.value?.description ?? "" )
                            print( " ============== ")
                        }
                        successHandler(model as AnyObject)
                    } else {
                        failureHandler(responseString.result.error)
                    }
                } else {
                    failureHandler(responseString.result.error)
                }
            } else {
                failureHandler(responseString.result.error)
            }
        }
    }
    
    
}
